require_relative "parsers/wein.cc.rb"
require "csv"
require "logger"

$log = Logger.new(STDOUT)
$log.level = Logger::DEBUG

# ARGs handling
newWines = Array.new(0)

# read CSV and iterate over all eans
# Note: CSV is UTF-8 encoded!
CSV.foreach("test.csv", :headers => true, :col_sep => ";", :encoding => "UTF-8") do |csv_wine|
  
  # because of corrupt/incomplete test.csv skip emtpy EANs
  next if csv_wine["EAN-Code"].nil?
  $log.info "CSV: Found wine " + csv_wine["Name"] + " EAN: " + csv_wine["EAN-Code"]
  $log.info "Starting search"
  
 # newWines += WineCc.getWines(csv_wine["EAN-Code"])
  $log.info "Added Wine to internal database"
end

#ean = "5604135001144"
#newWines += WineCc.getWines(ean)
#
ean = "5601237000129"
newWines += WineCc.getWines(ean)

ean = "5601237241225"
newWines += WineCc.getWines(ean)


$log.info "We found " + newWines.size.to_s + " wines"

# write cvs

headers= newWines[0].to_hash.keys
CSV.open("output.csv", "wb", :col_sep => ";", :encoding => "UTF-8") do |csv|
  csv << headers
  newWines.each do |w|
    w.to_hash.each {|elem| csv << elem}
  end
end


